<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php include("menu.php"); 
include("../connect/connect.php");
?>

<script>
function setFocus(){
document.formadd.prod_name.focus();
}
</script>
<script type="text/javascript" src="../sale/jquery-autocomplete/lib/jquery.js"></script>
<script type="text/javascript" src="../sale/jquery-autocomplete/jquery.autocomplete.js"></script>
<script type="text/javascript" src="../sale/jquery-autocomplete/lib/jquery.bgiframe.min.js"></script>
<script type="text/javascript" src="../sale/jquery-autocomplete/lib/jquery.ajaxQueue.js"></script>
<script type="text/javascript" src="../sale/jquery-autocomplete/lib/thickbox-compressed.js"></script>
<link rel="stylesheet" href="../sale/jquery-autocomplete/jquery.autocomplete.css"/>
 <?php
$Query = "SELECT prod_name FROM product WHERE prodtype_id between 2 and 3";
$objQuery = mysql_query($Query) or die("Error Query [" . $Query . "]");
	while($row = mysql_fetch_assoc($objQuery)){
		$line1[]=array($row["prod_name"]);
	}
?> 
<script type="text/javascript">
$(document).ready(function(){

	var str_month = <?php echo json_encode($line1).';'; ?>

	$("#prod_name").autocomplete( str_month , {
		mustMatch: false,
		autoFill: false,
		matchContains:true,
		});
})
</script>
</head>
<body onload="setFocus()">
<?php

if($_GET["action"]==add) {

	if($_POST["prod_name"]!="" && $_POST["par"]!="")
		{
		$checkcode = "SELECT prod_id FROM product WHERE prod_name = '".$_POST["prod_name"]."' AND prodtype_id between 2 and 3 ";
		$result = mysql_query($checkcode) or die ("'".checkcode."'");
		$row = mysql_num_rows($result);
		$rowID = mysql_fetch_array($result);
		
		$checkID = mysql_query("SELECT prod_id FROM stock_bar WHERE prod_id = '".$rowID['prod_id']."'");
		$NumRow = mysql_num_rows($checkID);
		
			if($NumRow>='1'){
				$save="สินค้า ".$_POST["name"]." นี้มีอยู่แล้ว";
			} else {
				if ($row>='1'){
				$Query = mysql_query("INSERT INTO stock_bar (prod_id,stock_bar_par) VALUES ('".$rowID['prod_id']."','".$_POST["par"]."')");
					if($Query) {
						$save="บันทึกข้อมูลเรียบร้อยแล้ว";
					}
				} else {
				$save="ชื่อสินค้า ".$_POST["prod_name"]." นี้ไม่มีอยู่ในรายชื่อสินค้า";
			}
			}
		}
}
?>
<form action="?action=add" name="formadd" method="post" id="formadd">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>เพิ่มสต็อกบาร์</h1>
    <div class="buttons"><a onclick="formadd.submit();" class="button"><span>บันทึก</span></a><a onclick="location = 'product_manage.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
    <table class="form" id="form">
      <tr>
        <td>ชื่อสินค้า : </td>
        <td><input type="text" name="prod_name" id="prod_name" size="30" /></td>
      </tr>
      <tr>
        <td>Par : </td>
        <td><input type="text" name="par" id="par" onkeydown="if(event.keyCode==13){formadd.submit();}" /></td>
      </tr>
    </table>
	<?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</div>
</form>
<?php mysql_close(); ?>
</body>
</html>
